package com.gm.wholesale.module.settlement.ui.tab;

import android.widget.BaseAdapter;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.interfaces.IResultListener;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.order.view.PayMoneyView;
import com.gm.wholesale.module.order.view.RefundMoneyView;
import com.gm.wholesale.module.settlement.bean.CustomerSettlementBean;
import com.gm.wholesale.module.settlement.model.ReconciliationMode;
import com.gm.wholesale.module.settlement.ui.detail.SettlementDetailFragment;

public class UnPayListFragment extends BaseTabFragment {


    @Override
    protected BaseAdapter createTempBaseAdapter() {

        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_unpay_item) {
            @Override
            public void convert(ViewHolder viewHolder, final CustomerSettlementBean data) {
                viewHolder.setText(R.id.titleTv, data.getName());
                viewHolder.setText(R.id.createTimeTv, "创建时间：" + data.getCreateTime());
                viewHolder.setText(R.id.numberTv, "对账编号：" + data.getNo());
                double totalPrice;
                if (data.getTotalAmount() > 0) {
                    totalPrice = data.getTotalAmount();
                } else {
                    totalPrice = data.getShouldPayAmount();
                }
                viewHolder.setText(R.id.toatlPriceTv, "合计金额：" + NumberUtils.getMoneyWithUnitStrig(totalPrice));
                viewHolder.getConvertView().setOnClickListener(v -> SettlementDetailFragment.showUI(TabType.WAITE_TO_PAY, data, getActivity()));
                viewHolder.getView(R.id.toAccountBtn).setOnClickListener(v -> showPayView(data));
            }
        };
    }


    public void showPayView(CustomerSettlementBean customerSettlementBean) {
        if (customerSettlementBean.getShouldPayAmount() > 0) { //收款
            PayMoneyView payMoneyView = new PayMoneyView(getActivity());
            payMoneyView.showPayMoney(PayMoneyView.PAY_TYPE_UNPAY, customerSettlementBean);
            payMoneyView.setOnSubmitClickListener((payInfoBean, goodBeans) -> {
                ReconciliationMode.payOrder((IBaseView) getActivity(), customerSettlementBean.getId(), getPayData(payInfoBean, customerSettlementBean.getVersion()), new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        payResult();
                    }
                });

            });
            Popupwindow.showPopCenterView(getActivity(), payMoneyView);
        } else { //退款
            RefundMoneyView payMoneyView = new RefundMoneyView(getActivity());
            payMoneyView.showView(RefundMoneyView.PAY_TYPE_UNPAY, customerSettlementBean, new IResultListener<Boolean>() {
                @Override
                public void onResult(Boolean result) {
                    payResult();
                }
            });
            Popupwindow.showPopCenterView(getActivity(), payMoneyView);
        }


    }

    private void payResult() {
        EvenBusUtil.updateSettlemenList();
        Popupwindow.hiddenPop();
        ToastUtil.showToast("支付成功");
        refreshData();
    }

    @Override
    public int getStatus() {
        return TabType.WAITE_TO_PAY;
    }
}
